import { defHttp } from '@/utils/http/axios';
import { ErrorMessageMode } from '#/axios';
import { BaseResponse, PagerResponse } from '@/api/base';
import UserDO from '@/api/model/UserDO';
import UserQuery from '@/api/model/UserQuery';
import UserRequest from '@/api/model/UserRequest';

// 定义接口地址
enum Api {
  /** 批量删除 */
  BATCH_DELETE = '/user/user/batchDelete',
  /** 删除 */
  DELETE_BY_ID = '/user/user/delete/',
  /** 查询基本信息 */
  DETAIL = '/user/user/',
  /** 列表查询 */
  LIST = '/user/user/list',
  /** 分页查询 */
  PAGER = '/user/user/pager',
  /** 新增 */
  SAVE = '/user/user/save',
  /** 修改 */
  UPDATE = '/user/user/update/',
}

// 定义接口方法
// 批量删除
export function batchDelete(params: number[], mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<number>>(
    {
      url: Api.BATCH_DELETE,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 删除
export function deleteById(id: number, mode: ErrorMessageMode = 'message') {
  return defHttp.delete<BaseResponse<boolean>>(
    {
      url: Api.DELETE_BY_ID + '/' + id,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 查询基本信息
export function detail(id: number, mode: ErrorMessageMode = 'message') {
  return defHttp.get<BaseResponse<UserDO>>(
    {
      url: Api.DETAIL + '/' + id,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 列表查询
export function list(params: UserQuery, mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<Array<UserDO>>>(
    {
      url: Api.LIST,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 分页查询
export function pager(params: UserQuery, mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<PagerResponse<UserDO>>>(
    {
      url: Api.PAGER,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 新增
export function save(params: UserRequest, mode: ErrorMessageMode = 'message') {
  return defHttp.post<BaseResponse<number>>(
    {
      url: Api.SAVE,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}
// 修改
export function update(id: number, params: UserRequest, mode: ErrorMessageMode = 'message') {
  return defHttp.put<BaseResponse<boolean>>(
    {
      url: Api.UPDATE + '/' + id,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

// 聚合接口
export default {
  batchDelete,
  deleteById,
  detail,
  list,
  pager,
  save,
  update,
};
